package kt.com.bilin.coin.ui.activity

import android.media.MediaPlayer
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemBsAudioBinding
import kotlinx.android.synthetic.main.activity_base_refresh.*
import kt.com.bilin.coin.bean.BSAudioBean
import kt.com.bilin.coin.common.*
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.ui.dialog.LoadingDialog


@Route(path = PATH_BS_AUDIO)
class BSAudioActivity : BaseRefreshActivity<BSAudioBean>() {
    private var animation: RotateAnimation? = null
    private var lastClickPosition: Int? = null
    private var mLoadingDialog: LoadingDialog? = null
    private var mMediaPlayer: MediaPlayer? = null
    override fun initView() {
        mBaseRecyclerAdapter = BSAudioAdapter()
        super.initView()
        titleView.titleStr = R.string.text_business_school_ketang.getString()
        titleView.isShowLine = true
        refreshLayout.setEnableRefresh(false)
        refreshLayout.setEnableLoadMore(false)
        rootView.setBackgroundColor(R.color.white.toColor())
    }

    override fun onReload() {
        launchUI({
            mutableListOf<BSAudioBean>().apply {
                add(BSAudioBean("Eason老师分享《初讲比邻》", MP3_1, "2020-10-05 10:46:35"))
                add(BSAudioBean("Eason老师分享《矿机模式&加油优惠》", MP3_2, "2020-10-06 12:18:55"))
                add(BSAudioBean("Eason老师分享《全新社群，2.0版本即将上线》", MP3_3, "2020-10-06 09:06:14"))
                add(BSAudioBean("Eason老师分享《玩转社群，裂变万人》", MP3_4, "2020-10-06 09:39:43"))
                add(BSAudioBean("景旭东老师分享对比邻的看法与理解", MP3_5, "2020-10-06 18:22:41"))
            }
        }, mRefreshHelper.handApiObserver())
    }

    override fun onDestroy() {
        super.onDestroy()
        mMediaPlayer?.apply {
            stop()
            release()
            mMediaPlayer = null
        }
    }

    private fun startAnimation(img: ImageView) {
        animation?.cancel()
        animation = RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        animation?.apply {
            fillAfter = true
            repeatCount = Animation.INFINITE
            duration = 2000
            interpolator = LinearInterpolator()
            img.startAnimation(animation)
        }
    }

    inner class BSAudioAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemBsAudioBinding>, BSAudioBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemBsAudioBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemBsAudioBinding>, bean: BSAudioBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    tvTitle.text = title
                    tvTime.text = time
                    setViewClick(position, root)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            super.subOnClickAdapter(v, position)
            if (lastClickPosition != position) {
                lastClickPosition = position
                startAnimation((recyclerView.findViewHolderForAdapterPosition(position) as BaseViewHolder<ItemBsAudioBinding>).mViewBinding.ivPlayer)
                val mBSAudioBean = getItem(position)
                if (mMediaPlayer == null) {
                    mLoadingDialog = LoadingDialog(this@BSAudioActivity)
                    mMediaPlayer = MediaPlayer()
                    mMediaPlayer?.setOnPreparedListener {
                        it?.start()
                        mLoadingDialog?.dismiss()
                    }
                    mMediaPlayer?.setOnErrorListener { _, _, _ ->
                        mLoadingDialog?.dismiss()
                        false
                    }
                }
                mLoadingDialog?.show()
                mMediaPlayer?.apply {
                    stop()
                    reset()
                    setDataSource(mBSAudioBean.link)
                    prepareAsync()
                }
            }
        }
    }
}